<template>
  <div class="app-container">
    <el-row :gutter="20">

      <el-col  :xm="24">
        <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
          <el-form-item label="标题" prop="title">
            <el-input v-model="queryParams.userName" placeholder="请输入标题" clearable style="width: 240px" @keyup.enter="handleQuery" />
          </el-form-item>
          <el-form-item label="流程名称" prop="currentStepName">
            <el-input v-model="queryParams.phoneNumber" placeholder="请输入流程名称" clearable style="width: 240px" @keyup.enter="handleQuery" />
          </el-form-item>
        
          <el-form-item label="发起时间">
            <el-date-picker
              v-model="dateRange"
              style="width: 240px"
              type="daterange"
              range-separator="-"
              start-placeholder="开始日期"
              end-placeholder="结束日期"></el-date-picker>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="search" @click="handleQuery">{{ $t('btn.search') }}</el-button>
            <el-button icon="refresh" @click="resetQuery">{{ $t('btn.reset') }}</el-button>
          </el-form-item>
        </el-form>

        <el-row :gutter="5" class="mb8">
          

          <right-toolbar v-model:showSearch="showSearch" @queryTable="getList" :columns="columns"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="approvalList" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="标识" align="center" key="id" prop="id" v-if="columns.showColumn('id')" />
           <el-table-column prop="executionPointerId" label="当前流程" align="center" v-if=false />
          <el-table-column prop="title" label="标题" align="center" v-if="columns.showColumn('title')" />
		   <el-table-column prop="currentStepName" label="当前审批节点" align="center" v-if="columns.showColumn('currentStepName')" />
		    <el-table-column prop="businessCode" label="业务单据号" align="center" v-if="columns.showColumn('businessCode')" />
          <el-table-column prop="status" label="状态" align="center" v-if="columns.showColumn('status')" />
		  <el-table-column prop="creatorName" label="发起人" align="center" v-if="columns.showColumn('creatorName')" />
          <el-table-column label="发起时间" align="center" prop="creationTime" v-if="columns.showColumn('creationTime')" width="160"></el-table-column>
          <el-table-column label="操作" align="left" width="110" class-name="small-padding fixed-width">
            <template #default="scope" >
             
                <el-button v-for="item in scope.row.nodeOperates" text="{{ item.operateName }}" icon="Check" @click="nodeOperation(item.operateName,item.operateEvent,scope.row)">{{ item.operateName }}</el-button>  
             
            </template>
          </el-table-column>
        </el-table>
        <pagination :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" @pagination="getList" />
      </el-col>
    </el-row>

   
  </div>

  <!-- 驳回弹窗 -->
    <el-dialog title="驳回意见" v-model="refuseDialog.open" width="600px" append-to-body>
      <el-form :model="form"  label-width="100px">
        <el-row :gutter="20">
          <el-col :lg="24">
            <el-form-item label="驳回意见" prop="refuse">
              <el-input type="textarea" v-model="form.refuseContext" placeholder="请输入" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <el-button text @click="cancel">{{ $t('btn.cancel') }}</el-button>
        <el-button type="primary" @click="refuseApproval()">{{ $t('btn.submit') }}</el-button>
      </template>
    </el-dialog>
</template>

<script setup name="user">
import { getToken } from '@/utils/auth'
import { listApproval } from '@/api/workflow/approval'
import { WfSubmitOrderClick, WfRefuseOrderClick } from '@/api/workflow/orderApproval'
import { ConsoleLogger } from '@microsoft/signalr/dist/esm/Utils';

const { proxy } = getCurrentInstance()

const statusOptions = ref([])
const sexOptions = ref([])

proxy.getDicts('sys_normal_disable').then((response) => {
  statusOptions.value = response.result
})


const approvalList = ref([])

const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref('')
const dateRange = ref([])

const testOperator = ref([{'Id':'3a0f594d-8949-c7ea-5339-f691c2398e4e','operateName':'广州宝迪'},{'Id':'3a0f594e-9e52-6a91-6b26-6105a4f467d9','operateName':'唯惠集团'}])
const refuseDialog = reactive({
  // 是否显示弹出层
  open: false,
  // 回调的方法
  refuseAction: undefined,
  //操作名称
  operateName: undefined,

  executionPointerId:undefined

})

// 列显隐信息
const columns = ref([
  { key: 0, label: `标识`, visible: true, prop: 'id' },
  { key: 1, label: `标题`, visible: true, prop: 'title' },
  { key: 2, label: `流程名称`, visible: true, prop: 'currentStepName' },
  { key: 3, label: `业务单据号`, visible: true, prop: 'businessCode' },
  { key: 4, label: `状态`, visible: true, prop: 'status' },
  { key: 5, label: `发起人`, visible: true, prop: 'creatorName' },
  { key: 6, label: `发起时间`, visible: true, prop: 'creationTime' }
])

const data = reactive({
  form: {},
  queryParams: {
    pageIndex: 0,
    pageSize: 10,
    title: undefined,
    currentStepName: undefined
  }
  
})

const { queryParams, form, rules } = toRefs(data)

function nodeOperation(operateName,operateEvent,row) {
  //alert(operateName+operateEvent+row);
  //eval(operateEvent + `('${operateName}',${row});`);
  if (operateName == '驳回') {
    refuseDialog.open = true;
    refuseDialog.refuseAction = operateEvent;
    refuseDialog.executionPointerId = row.executionPointerId;
    refuseDialog.operateName = operateName;
    return;
  }
  switch(operateEvent)
  {
    case "orderOperation.submitClick": 
    let auditOptions = {
    "executionPointerId": row.executionPointerId,
        "pass": true,
        "remark": form.refuseContext,
        "operateName": operateName,
      }
      debugger;
      WfSubmitOrderClick(auditOptions)   
    break;
    default :
    break;

  }

  getList();
}

function refuseApproval()
{
  debugger;


  if (refuseDialog.refuseAction == "orderOperation.rejectClick")
  {
    let auditOptions = {
    "executionPointerId": refuseDialog.executionPointerId,
        "pass": false,
        "remark": "",
        "operateName": refuseDialog.operateName,
      }
    WfRefuseOrderClick(auditOptions);
    refuseDialog.open = false;
    getList();
  }
}




/** 查询流程列表 */
function getList() {
  loading.value = true
  listApproval(proxy.addDateRange(queryParams.value, dateRange.value)).then((res) => {
    loading.value = false
    approvalList.value = res.result.items
    total.value = res.result.totalCount
  })
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}
/** 重置按钮操作 */
function resetQuery() {
  dateRange.value = []
  proxy.resetForm('queryRef')
  queryParams.value.deptId = undefined
  proxy.$refs.deptTreeRef.setCurrentKey(null)
  handleQuery()
}


/** 选择条数  */
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.id)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 重置操作表单 */
// function reset() {
//   form.value = {
//     title: undefined,
//     currentStepName: undefined
//   }
//   proxy.resetForm('userRef')
// }
/** 取消按钮 */
function cancel() {
  refuseDialog.open = false
  form.value = {
    refuseContext:""
  }
}
/** 新增按钮操作 */

getList()
</script>
<style scoped>
.avatar {
  width: 40px;
}
</style>
